//
//  radioViewController.m
//  iNews
//
//  Created by lanou3g on 14-12-15.
//  Copyright (c) 2014年 张在青. All rights reserved.
//

#import "radioViewController.h"
#import "radioView.h"
#import "RadioHandel.h"
#import "radioDetailDataModel.h"
#import "DownRadio.h"
#import <AVFoundation/AVFoundation.h>

#import "RadTableViewCell.h"


#import "radioDetailViewController.h"
@interface radioViewController ()<UITableViewDataSource,UITableViewDelegate>
//@property(nonatomic,retain)AVPlayer *player;
//@property(nonatomic,retain)AVPlayerItem *playItem;


@end

@implementation radioViewController


- (void)dealloc
{
    [_rad release];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
           self.rad = [[[radioView alloc] initWithFrame:CGRectMake(0,89, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 89 - 44)] autorelease];
    }
    return self;
}

-(void)loadView{

    self.view = self.rad;
}

-(void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
    
    [[RadioHandel shareRadioData].player pause];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [[DownRadio downLoad] unarchive];
    
   
    //接受下载或删除时数据改变时候更新数据
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getTheMessageForLoadinView) name:@"loadownArray" object:nil];

    
    
    //设置代理
    self.rad.radioTab.dataSource = self;
    
    self.rad.radioTab.delegate = self;
    
    
    //注册cell
    
    [self.rad.radioTab registerClass:[RadTableViewCell class] forCellReuseIdentifier:@"cell_id"];
   
    
    
}
//跟新UI
-(void)getTheMessageForLoadinView
{
    [self.rad.radioTab  reloadData];
    
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    if ([DownRadio downLoad].loadownArray.count == 0 ) {
        return 1;
    }else{
        
        return [DownRadio downLoad].loadownArray.count;
    }

    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    RadTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell_id" forIndexPath:indexPath ];
    
    //自定义cell中but的点击事件.
//    [cell.staBut addTarget:self action:@selector(startAction) forControlEvents:UIControlEventTouchUpInside];
    
    if ([DownRadio downLoad].loadownArray.count == 0 ) {
        
        cell.lab.text = @"亲,您暂无下载";
        cell.lab.font = [UIFont systemFontOfSize:15];
        return cell;
        
    }else{
        
        cell.lab.text = [DownRadio downLoad].loadownArray[indexPath.row];
        
        cell.lab.font = [UIFont systemFontOfSize:15];
        //最后加入自适应文本高度.
        
        return cell;
    }
}


//-(void)startAction
//{
//    
//    
//    
//}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    //这里是将页面跳转到播放页面的播放器.
    radioDetailViewController *a = [[[radioDetailViewController alloc] init] autorelease];
    [self.navigationController  pushViewController:a animated:YES];

    self.tabBarController.tabBar.hidden = YES;
    
    self.tabBarController.tabBarItem.image = nil;
    
    self.tabBarController.tabBarItem.title = nil;
    
    
    
    //这是播放的路径,不能正常播放.
    NSString *str =  [DownRadio downLoad].loadownArray[indexPath.row];
    
    
    NSString *document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    
    NSString *file = [document stringByAppendingString:@"/Radio/"];
    
    
    NSString *path = [file stringByAppendingFormat:@"%@.mp3",str];
    
//
    //NSURL *url = [NSURL fileURLWithPath:path];
    
//    self.playItem = [[AVPlayerItem alloc] initWithURL:url];
//    
//    self.player = [AVPlayer playerWithPlayerItem:self.playItem];
    
    [[RadioHandel shareRadioData]  StarPlayerWithString:path];
    
    [[RadioHandel shareRadioData].player  pause];
    
}


//处于编辑状态
-(UITableViewCellEditingStyle )tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    return self.styl;
    
}
//完成编辑
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //删除点击的cell ,title.
    if ([DownRadio downLoad].loadownArray.count == 0 ) {
        
        
    }else{
        
        //删除MP3文件
        NSString *document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
        
        NSString *filePath = [document stringByAppendingString:@"/Radio"];
        
        NSString *name = [filePath stringByAppendingFormat:@"/%@.mp3",[DownRadio downLoad].loadownArray[indexPath.row]];
        NSLog(@"name:%@",name);
        
        [[NSFileManager defaultManager]  removeItemAtPath:name error:nil];
        
        //删除title;
        [[DownRadio downLoad] delete:[DownRadio downLoad].loadownArray[indexPath.row]];
        
    }
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
